﻿using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using DataAccess;
using QuanLyKhachSan.Validation;

namespace QuanLyKhachSan
{
    public class ChiTietHoaDonDVManager : IValidator<ChiTietHoaDonDichVu>
    {
        public ValidateResult Validate(ChiTietHoaDonDichVu obj)
        {
            var validateResult = new ValidateResult();

            if (obj.SoLuong <= 0)
            {
                validateResult.Message.Add("Số lượng phải lớn hơn không");
            }
            return validateResult;
        }

        public ActionResult Add(ChiTietHoaDonDichVu obj)
        {
            var actionResult = new ActionResult();
            try
            {
                using (var context = new QLKhachSanContext())
                {
                    var ct =
                        context.ChiTietHoaDonDichVus.FirstOrDefault(
                            d => d.MaDV.Equals(obj.MaDV) && d.MaHoaDonDV.Equals(obj.MaHoaDonDV));
                    if (ct != null)
                    {
                        ct.SoLuong += obj.SoLuong;
                    }
                    else
                    {
                        context.ChiTietHoaDonDichVus.Add(obj);
                    }
                    context.SaveChanges();
                    HoaDonDichVuManager.UpdateMoney(obj.MaHoaDonDV);
                }
            }
            catch (Exception ex)
            {
                actionResult.Exceptions.Add(ex);
            }
            return actionResult;
        }

        public ActionResult Delete(string ma)
        {
            return null;
        }

        public ActionResult Delete(ChiTietHoaDonDichVu orderDetails)
        {
            var acResult = new ActionResult();
            using (var context = new QLKhachSanContext())
            {
                using (DbContextTransaction dbTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        context.Database.ExecuteSqlCommand(@"delete ChiTietHoaDonDichVu where MaDV =" +
                                                           orderDetails.MaDV + " and MaHoaDonDV =" +
                                                           orderDetails.MaHoaDonDV);
                        dbTransaction.Commit();
                        HoaDonDichVuManager.UpdateMoney(orderDetails.MaHoaDonDV);
                    }
                    catch (Exception ex)
                    {
                        dbTransaction.Rollback();
                        acResult.Exceptions.Add(ex);
                    }
                }
            }
            return acResult;
        }

        public ActionResult InsertOrUpdate(ChiTietHoaDonDichVu orderDetails){
            var actionResult = new ActionResult();
            using (var context = new QLKhachSanContext())
            {
                try
                {
                    context.ChiTietHoaDonDichVus.AddOrUpdate(orderDetails);
                    context.SaveChanges();
                    HoaDonDichVuManager.UpdateMoney(orderDetails.MaHoaDonDV);
                }
                catch (Exception ex)
                {
                    actionResult.Exceptions.Add(ex);
                }
            }
            return actionResult;
        }

        public ActionResultAsList<ChiTietHoaDonDichVu> Find(Expression<Func<ChiTietHoaDonDichVu, bool>> predicate)
        {
            var acResult = new ActionResultAsList<ChiTietHoaDonDichVu>();
            try
            {
                var context = new QLKhachSanContext();
                {
                    acResult.Result = context.ChiTietHoaDonDichVus.Where(predicate).ToList();
                    if (!acResult.Result.Any())
                        acResult.Exceptions.Add(new Exception("Không tìm thấy chi tiết hóa đơn dịch vụ nào"));
                }
            }
            catch (Exception ex)
            {
                acResult.Exceptions.Add(ex);
            }
            return acResult;
        }

        public ActionResultAsList<ChiTietHoaDonDichVu> GetAllOrdersDetail()
        {
            var listChiTiet = new ActionResultAsList<ChiTietHoaDonDichVu>();
            try
            {
                var context = new QLKhachSanContext();
                listChiTiet.Result = context.ChiTietHoaDonDichVus.ToList();

                return listChiTiet;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ActionResultAsList<ChiTietHoaDonDichVu> GetServicesByCustomer(int maHd)
        {
            var result = new ActionResultAsList<ChiTietHoaDonDichVu>();
            using (var context = new QLKhachSanContext())
            {
                result.Result = context.ChiTietHoaDonDichVus.Where(ct => ct.MaHoaDonDV.Equals(maHd)).ToList();
            }
            return result;
        }
    }
}

